#include "captcha.h"
#include <QMouseEvent>
#include <QPainter>
#include <QSize>
#include <QPushButton>
#include <QProcess>
#include <QLabel>
#include <QtDebug>
captcha::captcha(QWidget *parent) : QWidget(parent)
{
    setMouseTracking(true);
    this->press = false;
    this->setMinimumSize(500,500);
    this->setMaximumSize(500,500);
    this->setWindowTitle("手写验证框");
    QPushButton *btn1 = new QPushButton("确定",this);
    btn1->move(70,450);
    QPushButton *btn2 = new QPushButton("清除",this);
    btn2->move(370,450);
    QLabel * resultLabel = new QLabel("RESULT",this);
    resultLabel->move(250,450);
    QLabel * tipLabel = new QLabel("当手写板识别出的内容为3时，通过验证",this);
    tipLabel->move(60,5);
    connect(btn1,&QPushButton::clicked,this,[=]() {
        QRect rect(30,30,440,400);
        QPixmap pixmap = QWidget::grab(rect);     //把Widget窗口内容转为QPixmap
        QImage image = pixmap.toImage();
        QString imagePath = "/home/a1/qt/CHAT/Server/temp.png";
        image.save(imagePath);

        // 创建QProcess实例，并设置Python脚本的路径、参数和工作目录
        QProcess * process = new QProcess(this);
        process->setProgram("python");
        process->setArguments({"train.py"});
        process->setWorkingDirectory("/home/a1/qt/CHAT/Server/python");

        // 启动进程并等待完成
        process->start();
        if (!process->waitForFinished()) {
            qDebug() << "Python script execution failed!";
            return;
        }

        // 从进程标准输出中读取识别结果，并显示在界面上
        QString output = process->readAllStandardOutput().trimmed();
        x = output.at(output.size() - 1);
        resultLabel->setText(x);
    } );
    connect(btn2,&QPushButton::clicked,this,[=]() {
        point_list.clear();     //清空鼠标坐标记录
        update();               //更新，让画笔画画
        resultLabel->setText("Result");
    });

}


//绘画，每次update的时候执行
void captcha::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);         //创建画板，画板附着在对象this上，也就是Widget窗口
    QRect rect(30,30,440,400);
    painter.setWindow(rect);
    painter.setViewport(rect);
    painter.fillRect(rect, Qt::black);

    QPen mypen;                     //创建画笔
    mypen.setWidth(10);             //点的大小
    mypen.setColor(Qt::white);      //点的颜色
    painter.setPen(mypen);          //画板设置画笔

    int i = 0;                      //把每一次鼠标移动的地方进行画点
    while(point_list.size()!=0 && i != point_list.size())
    {
        painter.drawPoint(point_list.at(i));
        i++;
    }
}

//鼠标左键按下，开始记录坐标
void captcha::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        this->press = true;
    }
}

//鼠标移动事件
void captcha::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        if(this->press)     //只有鼠标被按下的时候才记录坐标
        {
            point = event->pos();       //记录坐标
            point_list.append(point);   //记录每一次移动的坐标
            update();                   //更新，让画笔画画
        }

    }
}

//鼠标松开，停止记录
void captcha::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        this->press = false;
    }
}

QString captcha::get_x()
{
    return x;
}

